/**
 * 
 */
package com.risetek.tft.action;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.octo.captcha.service.image.ImageCaptchaService;
import com.risetek.tft.context.Constants;
import com.risetek.tft.context.SecurityLog;
import com.risetek.tft.entity.TManager;
import com.risetek.tft.service.ManagerService;
import com.risetek.tft.service.RoleService;
import com.risetek.tft.service.SysconfigService;
import com.zlinepay.framework.web.util.RequestUtils;

/**
 * @author hetao
 * 
 */
@Controller
public class LoginAction extends BaseAction {
	
	@Autowired
	private SysconfigService sysconfigService;
	@Autowired
	private RoleService roleService;

	@SecurityLog(descr = "", logOperate = "") 
	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request, ModelMap model) {
		//sysconfigService.test();
		/*TRole role = new TRole();
		TManager manager = new TManager();
		manager.setAccount("fdf");
		manager.setPassword("f");
		manager.setState(true);
		manager.setName("112");
		TManager manager2 = new TManager();
		manager2.setAccount("fdf");
		manager2.setName("112");
		manager2.setState(true);
		manager2.setPassword("f");
		Set set = new HashSet();
		set.add(manager);
		set.add(manager2);
		role.setName("ttt");
		role.setTManagers(set);
		role.setRemark("");
		role.setCode("fdfdsfs");
		roleService.save(role);*/
		return "login";
	}
	
	@RequestMapping(value = "/dologin")
	public String submit(String username ,String password,
			  HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
				DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TManager.class);
				String password2 = DigestUtils.md5Hex(password);
				detachedCriteria.add(Restrictions.eq("account",username));
				detachedCriteria.add(Restrictions.eq("password", password2));
				TManager member = (TManager) managerService.queryForObject(detachedCriteria);
				if (member == null) {
					model.addAttribute("mesg", "用户名或密码错误！");
					return "login";
				} else if (!member.getState()) {
					model.addAttribute("mesg", "帐号已被停用！");
					return "login";
				} else {
					Timestamp lastlogintime = member.getLastlogintime();
					String lastloginip = member.getLastloginip();
					member.setLastloginip(RequestUtils.getIpAddr(request));
					member.setLastlogintime(new Timestamp(System.currentTimeMillis()));
					int logintime = member.getLogintimes() == null ? 0 : member.getLogintimes();
					member.setLogintimes(logintime+1);
					managerService.update(member);
					HttpSession session = request.getSession();
					session.setAttribute("lastlogintime", lastlogintime);
					session.setAttribute("lastloginip", lastloginip);
					session.setAttribute(Constants.USERID_IN_SESSION, member.getManagerid());
					System.out.println(cfg.toString());
					return "redirect:main."+(StringUtils.isEmpty(cfg.get("suffix")) ? "action" : cfg.get("suffix"));
				}
	}

	@RequestMapping(value = "/logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		HttpSession session = request.getSession();
		session.removeAttribute(Constants.USERID_IN_SESSION);
		session.invalidate();
		return "redirect:login."+(StringUtils.isEmpty(cfg.get("suffix")) ? "action" : cfg.get("suffix"));
	}
	
/*
	@RequestMapping(value = "/dologin")
	public String submit(String username ,String password,
			  HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		   AuthenticationToken token = new com.risetek.tft.shiro.AuthenticationToken(username, password, false, null);  
		   Subject subject = SecurityUtils.getSubject();
		   try {
			subject.login(token);
		} catch (AuthenticationException e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "redirect:login.htm";
		}
		return "redirect:main.htm";
		 
	} 
*/
 

	public static void main(String[] args) {
		String pwd = DigestUtils.md5Hex("daqsoft");
		System.out.println(pwd);
		// d80f699c062c8662fad3df86024e246c
	}

	/*@Autowired
	private ImageCaptchaService imageCaptchaService;*/
	@Autowired
	private ManagerService managerService;

}
